<?php
/**
 * Created by JetBrains PhpStorm.
 * User: quangvu
 * Date: 5/25/13
 * Time: 8:33 AM
 * To change this template use File | Settings | File Templates.
 */

class Loader extends BaseLoader {

    public function model($name) {
        $file = PATH_MODELS . $name . 'Model.php';
        $model = $name.'Model';
        if(is_readable($file)) {
            require_once($file);
            return new $model;
        }
        return false;
    }

    public function view($layout=null, $data) {
        $file = null;
        if(isset($layout)) {
            $file = PATH_VIEWS . $layout ;
        }else {
            $file = PATH_VIEWS . DEFAULT_VIEW_LAYOUT ;
        }
        if(is_readable($file)){
            if(is_array($data)){
                extract($data);
            }
            require $file;
        }
    }

    public function controller($name) {
        $file = PATH_CONTROLLERS . $name . 'Controller.php';
        $controller = $name.'Controller';
        if(is_readable($file)) {
            require_once($file);
            return new $controller;
        }
        return false;
    }

    public function helper($name) {
        $file = PATH_HELPERS . $name . 'Helper.php';
        $helper = $name.'Helper';
        if(is_readable($file)) {
            require_once($file);
            return new $helper;
        }
        return false;
    }

}